package com.redheart.web.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.redheart.web.response.BaseResponse;
import com.redheart.web.shiro.ShiroUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
public class LoginController {
    @Autowired
    private Producer producer;

    @RequestMapping("login.html")
    public String login(){
        return "login";
    }

    @RequestMapping("captcha.jpg")
    public void captcha(HttpServletResponse response)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到shiro session
        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }

    @ResponseBody
    @RequestMapping(value = "/sys/login", method = RequestMethod.POST)
    public ResponseEntity<BaseResponse> login(String username, String password) {       //去掉验证码 2019-01-22
     /*   String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
        if(!captcha.equalsIgnoreCase(kaptcha)){
            return new ResponseEntity<BaseResponse>(new BaseResponse(400,"验证码不正确"), HttpStatus.OK);
        }*/
        try{
            Subject subject = ShiroUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            subject.login(token);
        }catch (UnknownAccountException e) {
            return new ResponseEntity<BaseResponse>(new BaseResponse(400,e.getMessage()), HttpStatus.OK);
        }catch (IncorrectCredentialsException e) {
            return new ResponseEntity<BaseResponse>(new BaseResponse(400,"账号或密码不正确"), HttpStatus.OK);
        }catch (LockedAccountException e) {
            return new ResponseEntity<BaseResponse>(new BaseResponse(400,"账号已被锁定,请联系管理员"), HttpStatus.OK);
        }catch (AuthenticationException e) {
            return new ResponseEntity<BaseResponse>(new BaseResponse(400,"账户验证失败"), HttpStatus.OK);
        }
        return  new ResponseEntity<>(new BaseResponse(200,"success"),HttpStatus.OK);
    }



    /**
     * 退出
     */
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout() {
        ShiroUtils.logout();
        return "redirect:login.html";
    }
}
